package com.accounting.client.frontend.system;

import com.accounting.client.ApplicationContext;
import com.accounting.client.remote.Services;
import com.accounting.client.remote.transfer.CalculateResponse;
import com.accounting.client.session.ErrorHandler;
import com.accounting.client.session.SessionManager;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Widget;

import corelib.client.ui.widget.Size;
import corelib.client.ui.widget.WidgetListener;
import corelib.client.ui.widget.WindowHandle;
import corelib.client.ui.widget.button.Button;
import corelib.client.ui.widget.button.IsButton;
import corelib.client.ui.widget.dialog.SimpleDialogHandler;
import corelib.client.ui.widget.form.DecimalBox;
import corelib.client.ui.widget.form.SimpleLabel;
import corelib.client.ui.widget.layout.RelativeColumn;

public class CalculateHandler extends SimpleDialogHandler {
	
	private DecimalBox currencyField = new DecimalBox();
	private ReadOnlyCurrencyField totalCostField	     = ReadOnlyCurrencyField.create();
	private ReadOnlyCurrencyField totalOtherCostField    = ReadOnlyCurrencyField.create();
	private ReadOnlyCurrencyField totalPostageField      = ReadOnlyCurrencyField.create();
	private ReadOnlyCurrencyField totalAllCostField		 = ReadOnlyCurrencyField.create();
	private ReadOnlyCurrencyField totalProfitField       = ReadOnlyCurrencyField.create();
	private ReadOnlyCurrencyField totalOrderPostageField = ReadOnlyCurrencyField.create();
	private ReadOnlyCurrencyField totalOrderCostField    = ReadOnlyCurrencyField.create();
	private ReadOnlyCurrencyField totalOrderPriceField   = ReadOnlyCurrencyField.create();
	private ReadOnlyCurrencyField totalOrderProfitField  = ReadOnlyCurrencyField.create();
	private ReadOnlyCurrencyField accountCostField    	 = ReadOnlyCurrencyField.create();
	private ReadOnlyCurrencyField accountPostageField    = ReadOnlyCurrencyField.create();
	private ReadOnlyCurrencyField totalInvestmentField   = ReadOnlyCurrencyField.create();
	
	@Override
	public String getHeading() {
		return "计算明细";
	}

	@Override
	public Size getSize() {
		return new Size(650, 700);
	}

	@Override
	public Widget getContentPanel() {
		FlowPanel panel = new FlowPanel();
		panel.getElement().getStyle().setPadding(20, Unit.PX);
		
		int lw  = 130;
		int fw  = 180;
		int gap = 8;
		
		currencyField.setDouble(ApplicationContext.getCurrentRate());
		RelativeColumn col = new RelativeColumn();
		col.setRowSpacing(12);
		
		col.addRow()
		   .addSpace(gap)
		   .add(new SimpleLabel("点击按钮计算").setBold(true), lw)
		   .addSpace(gap)
		   .add(currencyField, 120)
		   .addSpace(gap)
		   .add(new Button("重置", new WidgetListener(){
				@Override
				public void valueUpdated(Widget widget) {
					reset();
				}
		   }),100)
		   .addSpace(gap)
		   .add(new Button("计算", new WidgetListener(){
				@Override
				public void valueUpdated(Widget widget) {
					calc();
				}
		   }),100);
		
		col.addRow()
		   .addSpace(lw + gap + gap)
		   .add(new SimpleLabel("澳币").setBold(true),fw)
		   .addSpace(gap)
		   .add(new SimpleLabel("人民币").setBold(true),fw);
		
		col.addRow().add(new SimpleLabel("订单明细").setBold(true));
		
		col.addRow()
		   .addSpace(gap)
		   .add(new SimpleLabel("总订单成本"), lw)
		   .addSpace(gap)
		   .add(totalOrderCostField.getAUDField(), fw)
		   .addSpace(gap)
		   .add(totalOrderCostField.getRMBField(), fw);
		
		col.addRow()
		   .addSpace(gap)
		   .add(new SimpleLabel("总订单邮费"), lw)
		   .addSpace(gap)
		   .add(totalOrderPostageField.getAUDField(), fw)
		   .addSpace(gap)
		   .add(totalOrderPostageField.getRMBField(), fw);
		
		col.addRow()
		   .addSpace(gap)
		   .add(new SimpleLabel("总订单销售价格").setBold(true), lw)
		   .addSpace(gap)
		   .add(totalOrderPriceField.getAUDField(), fw)
		   .addSpace(gap)
		   .add(totalOrderPriceField.getRMBField(), fw);
		
		col.addRow()
		   .addSpace(gap)
		   .add(new SimpleLabel("总订单的利润").setBold(true), lw)
		   .addSpace(gap)
		   .add(totalOrderProfitField.getAUDField(), fw)
		   .addSpace(gap)
		   .add(totalOrderProfitField.getRMBField(), fw);
		
		col.addRow().add(new SimpleLabel("我的操作记录").setBold(true), lw+fw);
		
		col.addRow()
		   .addSpace(gap)
		   .add(new SimpleLabel("购买花费"), lw)
		   .addSpace(gap)
		   .add(accountCostField.getAUDField(), fw)
		   .addSpace(gap)
		   .add(accountCostField.getRMBField(), fw);
		
		col.addRow()
		   .addSpace(gap)
		   .add(new SimpleLabel("邮寄花费(包括库存邮费和订单邮费)"), lw)
		   .addSpace(gap)
		   .add(accountPostageField.getAUDField(), fw)
		   .addSpace(gap)
		   .add(accountPostageField.getRMBField(), fw);
		
		col.addRow().add(new SimpleLabel("账务明细").setBold(true));
		
		col.addRow()
		   .addSpace(gap)
		   .add(new SimpleLabel("总购买成本"), lw)
		   .addSpace(gap)
		   .add(totalCostField.getAUDField(), fw)
		   .addSpace(gap)
		   .add(totalCostField.getRMBField(), fw);
		
		col.addRow()
		   .addSpace(gap)
		   .add(new SimpleLabel("总邮费"), lw)
		   .addSpace(gap)
		   .add(totalPostageField.getAUDField(), fw)
		   .addSpace(gap)
		   .add(totalPostageField.getRMBField(), fw);
		
		col.addRow()
		   .addSpace(gap)
		   .add(new SimpleLabel("总投入(购买成品 和 邮费)"), lw)
		   .addSpace(gap)
		   .add(totalInvestmentField.getAUDField(), fw)
		   .addSpace(gap)
		   .add(totalInvestmentField.getRMBField(), fw);
		
		col.addRow()
		   .addSpace(gap)
		   .add(new SimpleLabel("其他成本"), lw)
		   .addSpace(gap)
		   .add(totalOtherCostField.getAUDField(), fw)
		   .addSpace(gap)
		   .add(totalOtherCostField.getRMBField(), fw);
		
		col.addRow()
		   .addSpace(gap)
		   .add(new SimpleLabel("所有成本").setBold(true), lw)
		   .addSpace(gap)
		   .add(totalAllCostField.getAUDField(), fw)
		   .addSpace(gap)
		   .add(totalAllCostField.getRMBField(), fw);
		
		col.addRow()
		   .addSpace(gap)
		   .add(new SimpleLabel("总利润").setBold(true), lw)
		   .addSpace(gap)
		   .add(totalProfitField.getAUDField(), fw)
		   .addSpace(gap)
		   .add(totalProfitField.getRMBField(), fw);
		
		panel.add(col.getPanel());
		return panel;
	}

	@Override
	public IsButton[] getButtons() {
		return new IsButton[0];
	}

	@Override
	public void onClosing(WindowHandle windowHandle) {
		windowHandle.close();
	}
	
	private void reset(){
		currencyField.setDouble(ApplicationContext.getCurrentRate());
		calc();
	}
	
	private void calc() {
		String passport = SessionManager.getPassport();
		Services.CalculateService.Calculate(passport,new AsyncCallback<CalculateResponse>(){
			@Override
			public void onFailure(Throwable caught) {
				ErrorHandler.handleServiceError(caught);
			}
			
			@Override
			public void onSuccess(CalculateResponse result) {
				
				double rate   	 = currencyField.getDouble(ApplicationContext.getCurrentRate());
				double salePrice = result.getTotalOrderPrice();
				double profit 	 = salePrice - rate *  (result.getTotalOrderCost() + result.getTotalOrderPostage() + result.getTotalOtherCost());
				double total     = salePrice - rate *  (result.getTotalCost() + result.getTotalPostage() + result.getTotalOtherCost());
				
				// Total Fields
				totalCostField.setAUD(result.getTotalCost(),rate);
				totalPostageField.setAUD(result.getTotalPostage(),rate);
				totalInvestmentField.setAUD(result.getTotalCost() + result.getTotalPostage(), rate);
				totalOtherCostField.setAUD(result.getTotalOtherCost(),rate);
				totalAllCostField.setAUD(result.getTotalCost() + result.getTotalPostage() + result.getTotalOtherCost(), rate);
				totalProfitField.setRMB(total, rate);
				
				// Order Fields
				totalOrderPostageField.setAUD(result.getTotalOrderPostage(),rate);
				totalOrderCostField.setAUD(result.getTotalOrderCost(),rate);
				totalOrderPriceField.setRMB(result.getTotalOrderPrice(),rate);
				totalOrderProfitField.setRMB(profit,rate);
				
				// User Fields
				accountCostField.setAUD(result.getAccountCost(),rate);
				accountPostageField.setAUD(result.getAccountPostage(),rate);
				
			}
		});
	}

}
